using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TrafficTrouble.Core
{
    public class IconButton
    {
        private static Vector2 Margin { get { return new Vector2(5, 0); } }

        private static Texture2D BorderTex, HighlightTex;
        private static SpriteFont IconFont, TextFont;
        public static void SetContent(GraphicsDevice device, SpriteFont iconFont, SpriteFont textFont)
        {
            IconFont = iconFont;
            TextFont = textFont;

            BorderTex = new Texture2D(device, 1, 1);
            BorderTex.SetData<Color>(new Color[] { Color.White });

            HighlightTex = new Texture2D(device, 1, 1);
            HighlightTex.SetData<Color>(new Color[] { Color.White * .4f });
        }

        private Vector2 _loc;
        public Vector2 Location
        {
            get { return _loc; }
            set { _loc = value; this.computeSize(); }
        }

        public string Icon { get; private set; }
        public string Text { get; private set; }

        private Vector2 size;
        public Vector2 Size { get { return size; } }
        private Vector2 IconLoc, TextLoc;

        public bool TextBelowIcon { get; private set; }

        public bool IsPressed { get; set; }

        public IconButton(string icon, string text, bool textBelowIcon)
            : this(icon, text, float.NegativeInfinity, float.NegativeInfinity, textBelowIcon) { }
        public IconButton(string icon, string text, Vector2 location, bool textBelowIcon)
        {
            this.Icon = icon;
            this.Text = text;
            this.TextBelowIcon = textBelowIcon;
            this.Location = location;
            this.computeSize();
            this.IsPressed = false;
        }
        public IconButton(string icon, string text, float x, float y, bool textBelowIcon)
            : this(icon, text, new Vector2(x, y), textBelowIcon) { }

        private void computeSize()
        {
            Vector2 iconSize = IconFont.MeasureString(this.Icon);
            Vector2 textSize = TextFont.MeasureString(this.Text);

            Vector2 contentSize = new Vector2(MathHelper.Max(iconSize.X, textSize.X), iconSize.Y + textSize.Y);

            this.size = contentSize + 2 * Margin;

            this.IconLoc = this.TextLoc = new Vector2();
            this.IconLoc.X = this.Location.X + (this.Size.X - iconSize.X) / 2f;
            this.TextLoc.X = this.Location.X + (this.Size.X - textSize.X) / 2f;

            Vector2 contentLoc = this.Location + (this.Size - contentSize) / 2f;
            if (this.TextBelowIcon)
            {
                this.IconLoc.Y = contentLoc.Y;
                this.TextLoc.Y = contentLoc.Y + iconSize.Y;
            }
            else
            {
                this.TextLoc.Y = contentLoc.Y;
                this.IconLoc.Y = contentLoc.Y + textSize.Y;
            }
        }

        public bool ContainsLocation(Vector2 location)
        {
            return (location.X >= this.Location.X) && (location.X < this.Location.X + this.Size.X)
                && (location.Y >= this.Location.Y) && (location.Y < this.Location.Y + this.Size.Y);
        }

        public void Draw(SpriteBatch g)
        {
            if (this.IsPressed)
                g.Draw(HighlightTex, new Rectangle((int)this.Location.X, (int)this.Location.Y, (int)this.Size.X, (int)this.Size.Y), Color.White);

            g.Draw(BorderTex, new Rectangle((int)this.Location.X, (int)this.Location.Y, 1, (int)this.Size.Y), Color.White);
            g.Draw(BorderTex, new Rectangle((int)this.Location.X, (int)this.Location.Y, (int)this.Size.X, 1), Color.White);
            g.Draw(BorderTex, new Rectangle((int)this.Location.X + (int)this.Size.X - 1, (int)this.Location.Y, 1, (int)this.Size.Y), Color.White);
            g.Draw(BorderTex, new Rectangle((int)this.Location.X, (int)this.Location.Y + (int)this.Size.Y - 1, (int)this.Size.X, 1), Color.White);

            g.DrawString(IconFont, this.Icon, this.IconLoc, Color.White);
            g.DrawString(TextFont, this.Text, this.TextLoc, Color.White);
        }
    }
}
